/**
 * 
 */
package com.youxin.cpcenter.pt.service.member;

import java.util.List;
import java.util.Map;

import com.youxin.busfoundation.bean.ent.MemberIdentityBean;
import com.youxin.busfoundation.common.es.bean.CompanyInfoBean;
import com.youxin.busfoundation.enums.EnterpriseIdentifyStatus;
import com.youxin.busfoundation.enums.EnterpriseManagerType;
import com.youxin.busfoundation.enums.EnterprisePassStatus;
import com.youxin.busfoundation.net.WebVisitor;
import com.youxin.cpcenter.bean.MemberQueryListBean;
import com.youxin.cpcenter.bean.member.MemberInfo;
import com.youxin.cpcenter.pt.bean.ResponseListMember;
import com.youxin.cpcenter.pt.bean.ResponseOutMessage;
import com.youxin.cpcenter.pt.common.PtConfig;
import com.youxin.cpcenter.pt.service.BaseOperation;
import com.youxin.cpcenter.pt.service.PtTask;
import com.youxin.foundation.utils.ArrayUtils;
import com.youxin.foundation.utils.JsonUtils;

/**
 * @author yingde.cao
 * @date 2017年12月24日
 * @version 1.0
 */
public class ListMember extends BaseOperation {
	public static ResponseListMember postMethod(MemberQueryListBean bean, CompanyInfoBean companyInfoBean) {
		String url = PtConfig.currentURL() + PtConfig.MEMBER_LIST_SUFFIX;//"/api/member/list";
		Map<String, String> tokenMap = PtTask.instanse().getTokenMap(companyInfoBean);
		String outJson = WebVisitor.instance().httpPostJsonBody(url, bean, tokenMap);
		ResponseListMember outBean = JsonUtils.stringToFasterxmlObject(outJson, ResponseListMember.class);
		logger.error(outBean.toString());
		long time=outBean.getResponseTime()-bean.getRequestTime();
		saveRequestTimeToRedis(PtConfig.MEMBER_LIST_SUFFIX, time);
		return outBean;
	}

	public static ResponseOutMessage auth(CompanyInfoBean companyInfoBean,
			ResponseListMember listMember) {
		String url = PtConfig.currentURL() + PtConfig.MEMBER_AUTH_SUFFIX;//"/api/member/edition/auth";
		MemberQueryListBean listBean = listMember.getData();
		if (listBean == null) {
			return null;
		}
		if (ArrayUtils.isEmpty(listBean.getResults())) {
			return null;
		}
		List<MemberInfo> memberList = listBean.getResults();
		MemberInfo memberInfo=findUnAuth(memberList);
		if(memberInfo==null)
		{
			return null;
		}
		MemberIdentityBean identityBean=new MemberIdentityBean();
		identityBean.setMemberId(memberInfo.getMemberId());
		Map<String, String> tokenMap = PtTask.instanse().getTokenMap(companyInfoBean);
		String outJson = WebVisitor.instance().httpPostJsonBody(url, identityBean, tokenMap);
		ResponseOutMessage outBean = JsonUtils.stringToFasterxmlObject(outJson, ResponseOutMessage.class);
		
		long time=outBean.getResponseTime()-identityBean.getRequestTime();
		saveRequestTimeToRedis(PtConfig.MEMBER_AUTH_SUFFIX, time);
		
		return outBean;
	}
	
	public static ResponseOutMessage unAuth(CompanyInfoBean companyInfoBean,
			ResponseListMember listMember) {
		String url = PtConfig.currentURL() + PtConfig.MEMBER_UNAUTH_SUFFIX;//"/api/member/edition/unauth";
		MemberQueryListBean listBean = listMember.getData();
		if (listBean == null) {
			return null;
		}
		if (ArrayUtils.isEmpty(listBean.getResults())) {
			return null;
		}
		List<MemberInfo> memberList = listBean.getResults();
		MemberInfo memberInfo=findAuth(memberList);
		if(memberInfo==null)
		{
			return null;
		}
		MemberIdentityBean identityBean=new MemberIdentityBean();
		identityBean.setMemberId(memberInfo.getMemberId());
		Map<String, String> tokenMap = PtTask.instanse().getTokenMap(companyInfoBean);
		String outJson = WebVisitor.instance().httpPostJsonBody(url, identityBean, tokenMap);
		ResponseOutMessage outBean = JsonUtils.stringToFasterxmlObject(outJson, ResponseOutMessage.class);

		long time=outBean.getResponseTime()-identityBean.getRequestTime();
		saveRequestTimeToRedis(PtConfig.MEMBER_UNAUTH_SUFFIX, time);
		
		return outBean;
	}
	
	public static ResponseOutMessage open(CompanyInfoBean companyInfoBean,
			ResponseListMember listMember) {
		String url = PtConfig.currentURL() + PtConfig.MEMBER_OPEN_SUFFIX;//"/api/member/edition/open";
		MemberQueryListBean listBean = listMember.getData();
		if (listBean == null) {
			return null;
		}
		if (ArrayUtils.isEmpty(listBean.getResults())) {
			return null;
		}
		List<MemberInfo> memberList = listBean.getResults();
		MemberInfo memberInfo=findUnOpen(memberList);
		if(memberInfo==null)
		{
			return null;
		}
		MemberIdentityBean identityBean=new MemberIdentityBean();
		identityBean.setMemberId(memberInfo.getMemberId());
		Map<String, String> tokenMap = PtTask.instanse().getTokenMap(companyInfoBean);
		String outJson = WebVisitor.instance().httpPostJsonBody(url, identityBean, tokenMap);
		ResponseOutMessage outBean = JsonUtils.stringToFasterxmlObject(outJson, ResponseOutMessage.class);

		long time=outBean.getResponseTime()-identityBean.getRequestTime();
		saveRequestTimeToRedis(PtConfig.MEMBER_OPEN_SUFFIX, time);
		
		return outBean;
	}
	
	public static ResponseOutMessage close(CompanyInfoBean companyInfoBean,
			ResponseListMember listMember) {
		String url = PtConfig.currentURL() + PtConfig.MEMBER_CLOSE_SUFFIX;// "/api/member/edition/close";
		MemberQueryListBean listBean = listMember.getData();
		if (listBean == null) {
			return null;
		}
		if (ArrayUtils.isEmpty(listBean.getResults())) {
			return null;
		}
		List<MemberInfo> memberList = listBean.getResults();
		MemberInfo memberInfo=findOpen(memberList);
		if(memberInfo==null)
		{
			return null;
		}
		MemberIdentityBean identityBean=new MemberIdentityBean();
		identityBean.setMemberId(memberInfo.getMemberId());
		Map<String, String> tokenMap = PtTask.instanse().getTokenMap(companyInfoBean);
		String outJson = WebVisitor.instance().httpPostJsonBody(url, identityBean, tokenMap);
		ResponseOutMessage outBean = JsonUtils.stringToFasterxmlObject(outJson, ResponseOutMessage.class);

		long time=outBean.getResponseTime()-identityBean.getRequestTime();
		saveRequestTimeToRedis(PtConfig.MEMBER_CLOSE_SUFFIX, time);
		
		return outBean;
	}
	
	

	public static MemberInfo findUnAuth(List<MemberInfo> memberList) {
		for(MemberInfo item:memberList){
			if(item.getEnterpriseManager().shortValue()==EnterpriseManagerType.Admin.getValue())
			{
				continue;
			}
			if(item.getEnterpriseIdentyStatus().shortValue()!=EnterpriseIdentifyStatus.Authenticated.getValue())
			{
				return item;
			}
		}
		return null;
	}
	
	public static MemberInfo findAuth(List<MemberInfo> memberList) {
		for(MemberInfo item:memberList){
			if(item.getEnterpriseManager().shortValue()==EnterpriseManagerType.Admin.getValue())
			{
				continue;
			}
			if(item.getEnterpriseIdentyStatus().shortValue()==EnterpriseIdentifyStatus.Authenticated.getValue())
			{
				return item;
			}
		}
		return null;
	}
	
	public static MemberInfo findUnOpen(List<MemberInfo> memberList) {
		for(MemberInfo item:memberList){
			if(item.getEnterpriseManager().shortValue()==EnterpriseManagerType.Admin.getValue())
			{
				continue;
			}
			if(item.getEnterprisePass().shortValue()!=EnterprisePassStatus.Pass.getValue())
			{
				return item;
			}
		}
		return null;
	}
	
	public static MemberInfo findOpen(List<MemberInfo> memberList) {
		for(MemberInfo item:memberList){
			if(item.getEnterpriseManager().shortValue()==EnterpriseManagerType.Admin.getValue())
			{
				continue;
			}
			if(item.getEnterprisePass().shortValue()==EnterprisePassStatus.Pass.getValue())
			{
				return item;
			}
		}
		return null;
	}

}
